<template>
  <el-container>
    <el-header>
      <el-button type="primary" @click="sdialogVisible = true"
        >添加账号</el-button
      >
      <el-dialog
        :visible.sync="sdialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <div class="main">
          <div>
            <el-input
              v-model="addbook.bookName"
              placeholder="请输入账号名"
            ></el-input>
          </div>
          <div>
            <el-input
              v-model="addbook.auother"
              placeholder="请输入号主名字"
            ></el-input>
          </div>
          <div>
            <el-input
              v-model="addbook.content"
              placeholder="请输入账号介绍"
            ></el-input>
          </div>
          <div>
            <el-input
              v-model="addbook.price"
              placeholder="请输入账号小时单价"
            ></el-input>
          </div>
          <div>
            <el-input
              v-model="addbook.type"
              placeholder="请输入游戏分类"
            ></el-input>
          </div>
          <div>
            <el-input
              v-model="addbook.total"
              placeholder="请输入账号数量"
            ></el-input>
          </div>
          <van-uploader :after-read="safterRead">
            <!-- <van-button icon="plus" type="primary">上传图片</van-button> -->
          </van-uploader>
          <div class="state">
            <el-radio v-model="addbook.state" label="1">手游</el-radio>
            <el-radio v-model="addbook.state" label="2">端游</el-radio>
          </div>
        </div>

        <span slot="footer" class="dialog-footer">
          <el-button @click="sdialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addbookdata">确 定</el-button>
        </span>
      </el-dialog>
      <div class="search">
        <el-input v-model="search" placeholder="请输入内容"></el-input>
        <el-button type="primary" plain @click="searchdata">搜索</el-button>
      </div>
    </el-header>
    <el-main>
      <el-table
        :data="tableData"
        :stripe="true"
        height="780"
        border
        style="width: 100%"
      >
        <el-table-column fixed prop="id" label="账号编号" width="110">
        </el-table-column>
        <el-table-column prop="bookName" label="账号" width="160">
        </el-table-column>
        <el-table-column prop="auother" label="号主" width="120">
        </el-table-column>
        <el-table-column prop="type" label="类型" width="130">
        </el-table-column>
        <el-table-column prop="content" label="介绍" width="430">
        </el-table-column>
        <el-table-column prop="price" label="小时单价" width="100">
        </el-table-column>
        <el-table-column prop="sum" label="剩余" width="140"> </el-table-column>
        <el-table-column prop="total" label="库存" width="140">
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >查看</el-button
            >
            <el-button type="text" size="small" @click="open(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        v-if="setdata"
        :visible.sync="adialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <div class="main">
          <div>
            <el-input
              v-model="setdata.bookName"
              placeholder="请输入内容"
            ></el-input>
          </div>
          <div>
            <el-input
              v-model="setdata.auother"
              placeholder="请输入内容"
            ></el-input>
          </div>
          <div>
            <el-input
              v-model="setdata.content"
              placeholder="请输入内容"
            ></el-input>
          </div>
          <div>
            <el-input
              v-model="setdata.type"
              placeholder="请输入内容"
            ></el-input>
          </div>
          <van-uploader :after-read="afterRead">
            <!-- <van-button icon="plus" type="primary">上传图片</van-button> -->
          </van-uploader>
          <div class="state">
            <el-radio v-model="setdata.state" label="1">手游</el-radio>
            <el-radio v-model="setdata.state" label="2">端游</el-radio>
          </div>
        </div>

        <span slot="footer" class="dialog-footer">
          <el-button @click="adialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="updata">确 定</el-button>
        </span>
      </el-dialog>
    </el-main>
  </el-container>
</template>

<script>
export default {
  name: "MyManagement",
  data() {
    return {
      tableData: null,
      adialogVisible: false,
      sdialogVisible: false,
      setdata: null,
      addbook: {
        bookName: "".trim(),
        state: "1",
        auother: "",
        content: "".trim(),
        type: "",
        img: "",
        total: "",
        price: "",
      },
      search: "",
      url: "http://192.168.35.36:3005",
    };
  },
  methods: {
    searchdata() {
      if (this.search.trim() == "") {
        this.$message("输入的值不能为空");
        return;
      } else {
        this.$axios
          .post(`${this.url}/book/search`, {
            keyword: this.search,
          })
          .then((content) => {
            this.tableData = content.data;
          });
      }
    },
    addbookdata() {
      this.$axios
        .post(`${this.url}/book/addbook`, {
          data: this.addbook,
        })
        .then((content) => {
          console.log(content);
          this.sdialogVisible = false;
          this.addbook = {
            bookName: "",
            state: "1",
            auother: "",
            content: "",
            type: "",
            img: "",
            total: "",
          };
          this.getdata();
        });
    },
    updata() {
      this.$axios
        .post(`${this.url}/book/setdata`, {
          data: this.setdata,
        })
        .then(() => {
          this.getdata();
          this.adialogVisible = false;
        });
    },
    afterRead(file) {
      this.$axios
        .post(
          `${this.url}/upload/geturl`,
          {
            photo: file.file,
            fileType: "IMAGE",
          },
          { headers: { "Content-Type": "multipart/form-data" } }
        )
        .then((content) => {
          this.setdata.img = content.data.data;
        });
    },
    safterRead(file) {
      this.$axios
        .post(
          `${this.url}/upload/geturl`,
          {
            photo: file.file,
            fileType: "IMAGE",
          },
          { headers: { "Content-Type": "multipart/form-data" } }
        )
        .then((content) => {
          this.addbook.img = content.data.data;
        });
    },
    open(row) {
      this.setdata = row;
      this.adialogVisible = true;
    },
    handleClose(done) {
      this.$confirm("确认关闭？").then(() => {
        this.setdata = null;
        this.addbook = {
          bookName: "",
          state: "1",
          auother: "",
          content: "",
          type: "",
          img: "",
          total: "",
        };
        done();
      });
    },
    handleClick(row) {
      this.$alert(
        `作者:<p>${row.auother}<p>
            <p>${row.content}<p>
            `,
        row.bookName,
        {
          dangerouslyUseHTMLString: true,
        }
      );
    },
    getdata() {
      this.$axios
        .get(`${this.url}/book/booklist`, {
          params: {
            state: 0,
          },
        })
        .then((content) => {
          this.tableData = content.data;
        });
    },
  },
  created() {
    this.getdata();
  },
};
</script>

<style lang="less" scoped>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}
.search {
  display: flex;
  height: 100%;
  align-items: center;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.el-dialog {
  .el-dialog__header {
    height: 150px;
  }
  .main {
    display: flex;
    flex-direction: column;
    div {
      display: flex;
    }
    .state {
      margin-top: 20px;
    }
    .el-input {
      height: 40px;
      margin-top: 20px;
    }
  }
}

.el-header {
  display: flex;
  justify-content: space-between;
}
</style>